from fastapi import APIRouter, Depends, Security
from fastapi.responses import JSONResponse

from myProfile.models.data.myProfile_models import Login, Intervetion
from myProfile.repository.interventions import IntervetionRepository
from myProfile.models.request.myProfile_request import IntervetionReq
from myProfile.db_config.odmantic_config import create_db_engine
from myProfile.security.secure import get_current_valid_user

router = APIRouter()


@router.post("/intervention/add")
async def add_intervention(req: IntervetionReq,
                           current_user: Login = Security(
                               get_current_valid_user, scopes=["user"]),
                           engine=Depends(create_db_engine)):
    intervention = Intervetion(**req.model_dump())
    repo: IntervetionRepository = IntervetionRepository(engine)
    result = await repo.add_to_profile(current_user.id, intervention)
    if result is True:
        return req
    else:
        return JSONResponse(
            content={"message": "add intervention problem encountered."},
            status_code=500)


@router.delete("/intervention/delete/{id}")
async def delete_intervention(id: int,
                              current_user: Login = Security(
                                  get_current_valid_user, scopes=["user"]),
                              engine=Depends(create_db_engine)):
    repo: IntervetionRepository = IntervetionRepository(engine)
    result = await repo.remove_from_profile(current_user.id, id)
    if result is True:
        return JSONResponse(
            content={"message": "delete intervention successfully"},
            status_code=201)
    else:
        return JSONResponse(
            content={"message": "delete intervention unsuccessfully"},
            status_code=500)


@router.get("/intervention/get")
async def get_intervention(current_user: Login = Security(
                                get_current_valid_user, scopes=["user"]),
                           engine=Depends(create_db_engine)):
    repo: IntervetionRepository = IntervetionRepository(engine)
    return await repo.get_from_profile(current_user.id)
